package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.EcologyPoolRecord;
import com.sc.nft.enums.EcologyPoolFlowTypeEnum;
import com.sc.nft.enums.EcologyPoolOperationTypeEnum;
import com.sc.nft.enums.EcologyPoolTypeEnum;
import com.sc.nft.mapper.EcologyPoolRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author 刘国航
 * @since 2024/7/15
 */
@Slf4j
@Repository
public class EcologyPoolRecordDao extends ServiceImpl<EcologyPoolRecordMapper, EcologyPoolRecord> {


    public int batchSaveEcologyPoolRecord(List<EcologyPoolRecord> list) {
        return baseMapper.batchSaveEcologyPoolRecord(list);
    }


    public Page<EcologyPoolRecord> recordPage(Page<EcologyPoolRecord> page, List<EcologyPoolTypeEnum> poolTypeList, EcologyPoolFlowTypeEnum flowType, Date startTime, Date endTime, Long daoId, Long userId) {

        LambdaQueryWrapper<EcologyPoolRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(EcologyPoolRecord::getOperationType, buildFilterOperationTypeList());
        queryWrapper.in(CollectionUtils.isNotEmpty(poolTypeList), EcologyPoolRecord::getPoolType, poolTypeList);
        queryWrapper.eq(Objects.nonNull(flowType), EcologyPoolRecord::getFlowType, flowType);
        queryWrapper.ge(Objects.nonNull(startTime), EcologyPoolRecord::getCreateTime, startTime);
        queryWrapper.le(Objects.nonNull(endTime), EcologyPoolRecord::getCreateTime, endTime);
        queryWrapper.and(Objects.nonNull(daoId), wrapper -> wrapper.eq(EcologyPoolRecord::getDaoId, daoId)
                .eq(EcologyPoolRecord::getPoolType, EcologyPoolTypeEnum.BRAND)
                .or()
                .eq(EcologyPoolRecord::getSourceDaoId, daoId)
                .eq(EcologyPoolRecord::getSourcePoolType, EcologyPoolTypeEnum.BRAND));
        queryWrapper.and(Objects.nonNull(userId), wrapper -> wrapper.eq(EcologyPoolRecord::getUserId, userId)
                .eq(EcologyPoolRecord::getPoolType, EcologyPoolTypeEnum.ACCOUNT)
                .or()
                .eq(EcologyPoolRecord::getSourceUserId, userId)
                .eq(EcologyPoolRecord::getSourcePoolType, EcologyPoolTypeEnum.ACCOUNT));
        queryWrapper.orderByDesc(EcologyPoolRecord::getCreateTime);
        queryWrapper.orderByDesc(EcologyPoolRecord::getId);
        return page(page, queryWrapper);
    }

    public Page<EcologyPoolRecord> recordPage(Page<EcologyPoolRecord> page, EcologyPoolTypeEnum poolType, Long daoId, Long userId) {
        LambdaQueryWrapper<EcologyPoolRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(EcologyPoolRecord::getOperationType, buildFilterOperationTypeList());
        queryWrapper.in(Objects.nonNull(poolType), EcologyPoolRecord::getPoolType, poolType);
        queryWrapper.eq(Objects.nonNull(daoId), EcologyPoolRecord::getDaoId, daoId);
        queryWrapper.eq(Objects.nonNull(userId), EcologyPoolRecord::getUserId, userId);
        queryWrapper.orderByDesc(EcologyPoolRecord::getCreateTime);
        queryWrapper.orderByDesc(EcologyPoolRecord::getId);
        return page(page, queryWrapper);
    }

    private List<EcologyPoolOperationTypeEnum> buildFilterOperationTypeList() {
        List<EcologyPoolOperationTypeEnum> filterOperationTypeList = new ArrayList<>();
        filterOperationTypeList.add(EcologyPoolOperationTypeEnum.PLUS);
        filterOperationTypeList.add(EcologyPoolOperationTypeEnum.REDUCE);
        filterOperationTypeList.add(EcologyPoolOperationTypeEnum.BACK_FLOW);
        filterOperationTypeList.add(EcologyPoolOperationTypeEnum.CONVERT);
        filterOperationTypeList.add(EcologyPoolOperationTypeEnum.RETURN_RED_PACK);
        filterOperationTypeList.add(EcologyPoolOperationTypeEnum.CREATE_RED_PACK);
        return filterOperationTypeList;
    }

}
